import { inject, injectable } from "inversify";
import { PrismaDB } from "../db";
import { UserDto } from "./user.dto";
import { plainToClass } from "class-transformer";
import { validate } from "class-validator";
import { JWT } from "../jwt";

@injectable()
export class UserService {
  constructor(
    @inject(JWT) private readonly JWT: JWT,
    @inject(PrismaDB) private readonly PrismaDB: PrismaDB) {

  }
  public async getList() {
    return await this.PrismaDB.prisma.user.findMany()
  }
  public async createUser(user: UserDto) {
    const userDto = plainToClass(UserDto, user)
    const errors = await validate(userDto)
    console.log(userDto);
    console.log(errors);
    if (errors.length) {
      return errors
    } else {
      const result = await this.PrismaDB.prisma.user.create({
        data: {
          name: user.name,
          email: user.email
        }
      })
      return {
        ...result,
        token: this.JWT.createToken(result)
      }
    }
    


  }

}